/**
 * Created by Administrator on 2017/7/19.
 */

let variable = require('../data/variable.js');
let gameData = require('../util/gameData.js');
let log = require('../util/log.js');
let util = require('../util/util.js');
let blued = require('./blued.js');
let lovelive = require('./lovelive.js');
let tourist = require('./tourist.js');
let weixin = require('./weixin.js')

let login = module.exports;

login.run = function (req, res) {
    res.header('Access-Control-Allow-Origin', '*');
    let data = util.getRequestParam(req);
    //if (!data.platform) {
    //    data.platform = 'ios';
    //}
    if (data.proxy=="tourist"){
        if (!gameData.d.config.touristLogin) {
            let result = {};
            result.code = 109;
            result.msg = '禁止游客登录';
            res.send(result).end();
            return false;
        } else {
            //游客登陆
            login.tourist(data.account, data.password, data.platform, function (err1, res1) {
                login.result(res, err1, res1, data);
            });
        }   
        return 
    }   
    
    if(data.proxy=="lovelive"){
        //爱生活登陆
        login.lovelive(data.access_token,data.groupid,data.platform, function (err1, res1) {
            login.result(res, err1, res1, data);
        });
        return 
    }   
    
    if(data.proxy=="blued"){
        login.blued(data.access_token, data.expire, data.platform, function (err1, res1) {
            login.result(res, err1, res1, null);
        });
        return 
    }
    
    if(data.proxy=="weixin"){
        //判断是不是授权登陆的
        //console.log(data);
        if(data.code && data.code!=""){
            login.weixin(null, 0, data.code , data.platform , function (err1, res1) {
                login.result(res, err1, res1, null);
            });
        }else{
            login.weixin(data.access_token, data.expire,null,data.platform, function (err1, res1) {
                login.result(res, err1, res1, null);
            });
        }
        return
    }
    
};
login.result = function (res, err, data, param) {
    let result = {};
    if (err != null) {
        result.code = 101;
        result.msg = '发生了不明的错误';
        res.send(result).end();
        return false;
    }
    if (data.error) {
        result.code = data.error;
        result.msg = data.msg;
        res.send(result).end();
        return false;
    }
    let randGate = Math.floor(Math.random() * variable.gate.length);
    result.code = 200;
    result.token = data.token;
    result.host = variable.gate[randGate].host;
    result.port = variable.gate[randGate].port;
    if (data.account) {
        result.isReg = true;
        result.account = data.account;
        result.password = data.password;
    } else if (param) {
        result.account = param.account;
        result.password = param.password;
    }
    log.d('登录成功', JSON.stringify(result));
    res.send(result).end();
    return false;
};
login.tourist = function (account, password, platform, cb) {
    log.p('游客登录', account, password);
    let touristClass = new tourist(account, password, platform);
    touristClass.run(function (err, res) {
        cb(err, res);
    });
};
login.blued = function (token, expire, platform, cb) {
    log.d('blued登录', token, expire);
    let bluedClass = new blued(token, expire, platform);
    bluedClass.run(function (err, res) {
        cb(err, res);
    });
};
login.lovelive = function (token, groupid, platform, cb) {
    log.d('lovelive登录', token);
    let loveliveClass = new lovelive(token,groupid, platform);
    loveliveClass.run(function (err, res) {
        cb(err, res);
    });
};
login.weixin = function (token, expire, code , platform, cb) {
    log.d('weixin登录', token, expire);
    let weixinClass = new weixin(token, expire, code,platform);
    weixinClass.run(function (err, res) {
        cb(err, res);
    });
};